﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Duck.Models;

namespace Duck.Controllers
{
    public class MONController : Controller
    {
        //
        // GET: /MON/
        chiecnhancoEntities db = new chiecnhancoEntities();
        #region[Create]
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(FormCollection collection, MON mon)
        {
            string sMamon = collection["txtMamon"];
            string sTenmon = collection["txtTenmon"];
            int iSotiet = int.Parse(collection["txtSotiet"]);
            int iThutu = int.Parse(collection["txtThutu"]);
            string Detail = collection["txtDetail"];
            string sDetail = Detail;
            string dDate = collection["txtDate"];
            string Date = dDate;


            mon.MA_MON = sMamon;
            mon.TEN_MON = sTenmon;
            mon.SO_TIET = iSotiet;
            mon.THUTU = iThutu;

            db.MONS.Add(mon);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        #endregion
        #region[Edit]
        public ActionResult Edit(int id)
        {
            var mon = db.MONS.First(m => m.ID == id);

            return View(mon);
        }
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var mon = db.MONS.First(m => m.ID == id);
            string sMamon = collection["MA_MON"];
            string sTenmon = collection["TEN_MON"];
            int iSotiet = int.Parse(collection["SO_TIET"]);
            int iThutu = int.Parse(collection["THUTU"]);

            mon.MA_MON = sMamon;
            mon.TEN_MON = sTenmon;
            mon.SO_TIET = iSotiet;
            mon.THUTU = iThutu;

            UpdateModel(mon);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        #endregion
        #region[View]
        public ActionResult Index()
        {
            var mon = (from m in db.MONS select m).ToList();
            return View(mon);
        }
        #endregion

    }
}
